package org.kazuhiko.rpg.manager;

import java.awt.Rectangle;
import java.util.HashMap;

import org.kazuhiko.rpg.core.menu.Menu;
import org.kazuhiko.rpg.core.menu.MenuItem;
import org.kazuhiko.rpg.core.types.MenuType;

public class MenuManager
{
	private static MenuManager instance;
	
	private HashMap<MenuType, Menu> menus;
	
	private Menu activeMenu;
	
	public static MenuManager getManager()
	{
		if( instance == null )
		{
			instance = new MenuManager();
		}
		
		return instance;
	}
	
	private MenuManager()
	{
		menus = new HashMap<MenuType, Menu>();
	}
	
	public void addMenu( Menu menu, MenuType type )
	{
		if( menus.containsKey( type ) )
		{
			menus.remove( type );
		}
		
		menus.put( type, menu );
	}
	
	public Menu getMenu( MenuType type )
	{
		Menu ret = null;
		
		if( menus.containsKey( type ) )
		{
			ret = menus.get( type );
		}
		
		return ret;
	}
	
	public void setActiveMenu( MenuType type )
	{
		if( menus.containsKey( type ) )
		{
			activeMenu = menus.get( type );
		}
		else
		{
			activeMenu = null;
		}
	}
	
	public Menu getActiveMenu()
	{
		return activeMenu;
	}
	
	public void checkButtons( Rectangle bounds )
	{		
		for( MenuItem item : activeMenu.getItems() )
		{
			if( bounds.intersects( item.getBounds() ) )
			{
				item.onClick();
			}
		}
	}
}
